Page({
  data: {
    notice: "这里是一条公告信息,提高城市管理水平",
    userInfo: null,
    isLogin: false,
    loading: true
  },

  onLoad() {
    this.checkLoginStatus();
    this.setData({ loading: false });
  },

  onShow() {
    const userInfo = wx.getStorageSync('userInfo');
    this.setData({ 
      isLogin: !!(userInfo && userInfo.isLogin),
      userInfo
    });
  },

  checkLoginStatus() {
    const userInfo = wx.getStorageSync('userInfo');
    
    if (!userInfo || !userInfo.isLogin) {
      this.setData({ isLogin: false });
      return;
    }
    
    this.setData({ isLogin: true });
    this.fetchUserInfo(userInfo.phone);
  },

  fetchUserInfo(phone) {
    wx.request({
      url: 'http://localhost:8080/api/userYZ/info',
      method: 'GET',
      data: { phone },
      success: (res) => {
        if (res.data && res.data.code === 200) {
          this.setData({ userInfo: res.data.data });
        }
      },
      fail: (err) => {
        console.error('获取用户信息失败:', err);
        wx.showToast({ title: '获取用户信息失败', icon: 'none' });
      },
      complete: () => {
        this.setData({ loading: false });
      }
    });
  },

  loadHomeData() {
    this.setData({ loading: false });
  },

  goToLogin() {
    wx.navigateTo({ url: '/pages/login/login' });
  }
});